package com.hdoit.ares.quartz;

import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.team.model.CooperationTeam;
import com.hdoit.ares.logical.team.service.ICooperationTeamService;
import com.hdoit.ares.selfcommon.enums.cust.CustomerLevel;
import com.tennetcn.common.annotation.QuartzTaskConfig;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.logical.setting.service.IParamOptionService;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.viewmodel.setting.ParamOptionSearch;

@QuartzTaskConfig(cronTrigger = "0 0 2,6,13 * * ?", jobTitle = "用来修改地区和自动创建高创后台账户")
public class QuartzHighMakerAccountJob implements Job{
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		ICustomerService customerService = CommonApplicationContextUtil.getCurrentContext().getBean(ICustomerService.class);
		//改变城市地区
		List<Customer> customers = customerService.queryList();
		List<Customer> custs = new ArrayList<Customer>();
		
		for(int i = customers.size() -1; i >=0; i--){
			if(CustomerLevel.highMaker.equals(customers.get(i).getLevel())){
				custs.add(customers.get(i));
			}
			if(!StringUtils.isEmpty(customers.get(i).getCityId())){
				customers.remove(i);
			}
		}
		IParamOptionService paramOptionService =  CommonApplicationContextUtil.getCurrentContext().getBean(IParamOptionService.class);
		ParamOptionSearch paramOptionSearch = new ParamOptionSearch();
		
		if(customers != null && customers.size() >= 0){
		for(Customer customer:customers){
			String str = customer.getRegion();
			System.err.println(str);
			
			if(!StringUtils.isEmpty(str)){
				String[] strs = str.split(" ");
				paramOptionSearch.setParamValue(strs[0]);
				paramOptionSearch.setParentId(null);
				ParamOption paramOption = paramOptionService.queryModel(paramOptionSearch);
				
				if(paramOption != null && !"香港特别行政区".equals(strs[0]) && !"澳门特别行政区".equals(strs[0])){
					customer.setProvinceId(paramOption.getId());
				
				
					paramOptionSearch.setParamValue(strs[1]);
					paramOptionSearch.setParentId(paramOption.getId());
					ParamOption paramOption1 = paramOptionService.queryModel(paramOptionSearch);
					if(paramOption1 != null)
						customer.setCityId(paramOption1.getId());
				
						if(strs.length == 3){
							paramOptionSearch.setParamValue(strs[2]);
							paramOptionSearch.setParentId(paramOption1.getId());
							ParamOption paramOption2 = paramOptionService.queryModel(paramOptionSearch);
							
							if(paramOption2 != null){
								customer.setDistrictId(paramOption2.getId());
							}
						
						}
						customerService.updateModel(customer);
				   }
				}
				
			}
		}
			//为以前未加入高创，或者有遗漏的人创建后台账户
		
		ICooperationTeamService cooperationTeamService = CommonApplicationContextUtil.getCurrentContext().getBean(ICooperationTeamService.class);
		IEmployeeService employeeService = CommonApplicationContextUtil.getCurrentContext().getBean(IEmployeeService.class);
		List<Employee> employees = employeeService.queryList();  
		for(Customer customer:custs){
			boolean  bool = true;
			for(Employee employee:employees){
				 if(customer.getId().equals(employee.getEmployeeId())){
					 bool =false;
				 }
				 
			}
		
		    CooperationTeam cooperationTeam = new CooperationTeam();
			cooperationTeam.setEmployeeId(customer.getId());
			cooperationTeam.setEmployeeName(customer.getUsername());
			cooperationTeam.setEmployeeCode(""+customer.getUserIdentity());
			cooperationTeam.setEmployeeAccount(""+customer.getUserIdentity());
			cooperationTeam.setMobile(""+customer.getPhone());
			cooperationTeam.setUserIdentity(customer.getUserIdentity());
			cooperationTeam.setBusinessId("b80cb478-fbae-489a-ae6e-e1150200a918");
			cooperationTeam.setRoleIds("cfe03e08-d465-4010-8108-7704b046856e");
			cooperationTeam.setModelStatus(ModelStatus.add);
			if(bool == true){
				cooperationTeamService.applyChange(cooperationTeam);
			}
			
		
		  }
		  
		 }
		

}
